package com.dna.okangjang.main.web;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dna.okangjang.main.model.SearchResult;
import com.dna.okangjang.main.service.MainService;

@Controller
public class MainController {
	private static final Log logger = LogFactory.getLog(MainController.class);
	
	private static final int BLOG_ROW = 10;
	private static final int CAFE_ROW = 10;
	
	@Resource
	private MainService mainService;
	
	@RequestMapping("/main")
	public ModelAndView main(ModelAndView mav){
		
		mav.setViewName("main/main");
		return mav;
	}
	
	@RequestMapping("/blank/main/data")
	public ModelAndView data(ModelAndView mav
			, @RequestParam(value="page", required=true) int page
			){
		
		List<SearchResult> list = mainService.getBlogSearch(page, BLOG_ROW);
		list.addAll(mainService.getCafeSearch(page, CAFE_ROW));
		
		mav.addObject("data", list);
		mav.setViewName("main/data");
		return mav;
	}
	
	@RequestMapping("/main/search")
	public ModelAndView search(ModelAndView mav){
		
		mav.setViewName("main/search");
		return mav;
	}
	
	@RequestMapping("/blank/main/searchResult")
	public ModelAndView searchResult(ModelAndView mav
			, @RequestParam(value="page", required=true) int page
			, @RequestParam(value="query", required=true) String query
			){
		
		try {
			query = URLDecoder.decode(query, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.debug(">>> query : " + query);
		
		List<SearchResult> list = mainService.getBlogKeywordSearch(query, page, BLOG_ROW);
		list.addAll(mainService.getCafeKeywordSearch(query, page, CAFE_ROW));
		
		mav.addObject("data", list);
		mav.setViewName("main/searchResult");
		return mav;
	}
	
	@RequestMapping("/main/detail")
	public ModelAndView detail(ModelAndView mav
			, @RequestParam(value="title", required=true) String title
			, @RequestParam(value="imgUrl", required=true) String imgUrl
			, @RequestParam(value="link", required=true) String link
			, @RequestParam(value="description", required=true) String description
			, @RequestParam(value="pubDate", required=true) String pubDate
			, @RequestParam(value="target", required=true) String target
			, @RequestParam(value="author", required=true) String author){
		
		logger.debug(">>> imgUrl : " + imgUrl);
		
		try {
			title = URLDecoder.decode(title, "UTF-8");
			description = URLDecoder.decode(description, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		SearchResult item = new SearchResult();
		item.setTitle(title);
		item.setImgUrl(imgUrl);
		item.setLink(link);
		item.setDescription(description);
		item.setPubDate(pubDate);
		item.setTarget(target);
		item.setAuthor(author);
		
		mav.addObject("item", item);
		mav.setViewName("main/detail");
		return mav;
	}
	
	@RequestMapping("/main/introduce")
	public ModelAndView introduce(ModelAndView mav){
		
		mav.setViewName("main/introduce");
		return mav;
	}
	
	
}
